#!/bin/bash -e

/tools/bin/env -i \
    HOME=$1 \
    TERM=$2 \
    PS1=$3  \
    PATH=$4

# should the documentation be builded and installed?
MAKE_DOC=false


echo	$Home
echo	$TERM
echo	$PS1
echo	$PATH

#ln --version

#logout
#exit 0



echo	""
echo	"---------------------------------------------"
echo	" 6.5. Creating Directories "
echo	"---------------------------------------------"
echo	"
Creating standard directory tree"
mkdir -pv /{bin,boot,etc/{opt,sysconfig},home,lib,mnt,opt,run}
mkdir -pv /{media/{floppy,cdrom},sbin,srv,var}
install -dv -m 0750 /root
install -dv -m 1777 /tmp /var/tmp
mkdir -pv /usr/{,local/}{bin,include,lib,sbin,src}
mkdir -pv /usr/{,local/}share/{doc,info,locale,man}
mkdir -v  /usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -pv /usr/{,local/}share/man/man{1..8}
for dir in /usr /usr/local; do
  ln -sv share/{man,doc,info} $dir
done
case $(uname -m) in
 x86_64) ln -sv lib /lib64 && ln -sv lib /usr/lib64 ;;
esac
mkdir -v /var/{log,mail,spool}
ln -sv /run /var/run
ln -sv /run/lock /var/lock
mkdir -pv /var/{opt,cache,lib/{misc,locate},local}

 # The FHS also stipulates the existence of /usr/local/games and 
 # /usr/share/games. The FHS is not precise as to the structure of 
 # the /usr/local/share subdirectory, so we create only the 
 # directories that are needed. However, feel free to create 
 # these directories if you prefer to conform more strictly 
 # to the FHS. 

echo	""
echo	"---------------------------------------------"
echo	" 6.6. Creating Essential Files and Symlinks  "
echo	"---------------------------------------------"
echo	"
Some programs use hard-wired paths to programs 
which do not exist yet. In order to satisfy 
these programs, create a number of symbolic 
links which will be replaced by real files 
throughout the course of this chapter after 
the software has been installed."
ln -sv /tools/bin/{bash,cat,echo,pwd,stty} /bin
ln -sv /tools/bin/perl /usr/bin
ln -sv /tools/lib/libgcc_s.so{,.1} /usr/lib
ln -sv /tools/lib/libstdc++.so{,.6} /usr/lib
sed 's/tools/usr/' /tools/lib/libstdc++.la > /usr/lib/libstdc++.la
ln -sv bash /bin/sh

echo	"
A proper Linux system maintains a list of the 
mounted file systems in the file /etc/mtab"
touch /etc/mtab

echo	"
In order for user root to be able to login 
and for the name “root” to be recognized, 
there must be relevant entries in the 
/etc/passwd and /etc/group files."
cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF


cat > /etc/group << "EOF"
root:x:0:
bin:x:1:
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
mail:x:34:
nogroup:x:99:
EOF

echo	"
Start a new shell. Since a full Glibc was 
installed in Chapter 5 and the /etc/passwd 
and /etc/group files have been created, 
user name and group name resolution will now work"
exec /tools/bin/bash --login +h

echo	"
The login, agetty, and init programs (and others) 
use a number of log files to record information
such as who was logged into the system and when. 
However, these programs will not write to the
log files if they do not already exist. 
Initializing the log files and give them proper permissions."
touch /var/log/{btmp,lastlog,wtmp}
chgrp -v utmp /var/log/lastlog
chmod -v 664  /var/log/lastlog
chmod -v 600  /var/log/btmp




















CRTPKG=
TAR_OPTS=
TAR_TYPE=
TAR_OUT=
TMP_DIR=
SRC_DIR=
CRTLOG=
function 	initDirEx	{
	# $1 is the index to be printed
	# $2 is the file name
	# $3 is the file extension
	# $4 is the output directory from tar
	# $5 is a temp dir to create (may be empty)
	
	CRTPKG=$2
	TAR_OPTS=
	TAR_TYPE=$3
	TAR_OUT=$4
	TMP_DIR=$5
	CRTLOG=/sources/$CRTPKG.log
	SRC_DIR=/sources/$TAR_OUT
	echo	"
		Target $CRTPKG initialised at $(date)
		
		
	" >> $CRTLOG
	
	if [ "$TAR_TYPE" == "bz2" ]; then
		TAR_OPTS="jxf"
	elif [ "$TAR_TYPE" == "gz" ]; then
		TAR_OPTS="-zxf"
	elif [ "$TAR_TYPE" == "xz" ]; then
		TAR_OPTS="-Jxf"
	else
		echo	"ERROR!!! Unknown tar type: $TAR_TYPE"
		exit -1
	fi
	
	echo	""
	echo	""
	echo	"---------------------------------------------"
	echo	"  $1 $2"
	echo	"---------------------------------------------"
	echo	""
	echo	"Extracting /sources/$CRTPKG.tar.$TAR_TYPE"
	cd /sources
	tar $TAR_OPTS $CRTPKG.tar.$TAR_TYPE >> $CRTLOG
	cd ./$TAR_OUT
	
	if [ ! -z "$TMP_DIR" ]; then
		TMP_DIR="/sources/$TMP_DIR"
		mkdir -v $TMP_DIR
		cd $TMP_DIR
	fi
	echo	"Sources were extracted."
	
}

function 	initDir	{
	# $1 is the index to be printed
	# $2 is the file name without extension
	# $3 is the file extension
	# $4 is a temp dir to create (may be empty)
	initDirEx $1 $2 $3 $2 $4
}

function 	rDir	{
	# $1 is the source dir
	# $2 is a temp dir
	rm -rfv $SRC_DIR >> $CRTLOG
	if [ ! -z "$TMP_DIR" ]; then
		rm -rfv $TMP_DIR >> $CRTLOG
	fi
}

function 	finishDir	{
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	# $1 is the source dir
	# $2 is a temp dir
	rm -rfv $SRC_DIR >> $CRTLOG
	if [ ! -z "$TMP_DIR" ]; then
		rm -rfv $TMP_DIR >> $CRTLOG
	fi
}

function	doItAll		{
	# $1 is the index to be printed
	# $2 is the file name
	# $3 is the file extension
	# $4 is a temp dir to create (may be empty)
	initDir $1 $2 $3 $4
	./configure --prefix=/usr
	finishDir
}





# ===============================================================
# Linux headers
initDir "6.7." "linux-3.6.2" "xz"
	# Make sure there are no stale files and dependencies 
	# lying around from previous activity:
	make mrproper >> $CRTLOG

	# Now test and extract the user-visible kernel 
	# headers from the source. 
	# They are placed in an intermediate 
	# local directory and copied to the needed 
	# location because the extraction process removes 
	# any existing files in the target directory.

	make headers_check
	make INSTALL_HDR_PATH=dest headers_install
	find dest/include \( -name .install -o -name ..install.cmd \) -delete
	cp -rv dest/include/* /usr/include
	
rDir
# ===============================================================

initDir "6.8." "man-pages-3.42" "xz"
	make install
rDir
initDir "6.8." "man-pages-3.42" "xz" "glibc-build"
	pushd $SRC_DIR > /dev/null
		sed -i 's#<rpc/types.h>#"rpc/types.h"#' sunrpc/rpc_clntout.c
		patch -Np1 -i ../glibc-2.16.0-fix_test_installation-1.patch
		sed -i 's|@BASH@|/bin/bash|' elf/ldd.bash.in
		patch -Np1 -i ../glibc-2.16.0-res_query_fix-1.patch
	popd
	/sources/$CRTPKG/configure  \
		--prefix=/usr          \
		--disable-profile      \
		--enable-add-ons       \
		--enable-kernel=2.6.25 \
		--libexecdir=/usr/lib/glibc >> $CRTLOG
	make >> $CRTLOG
	make -k check 2>&1 | tee glibc-check-log >> $CRTLOG
	grep Error glibc-check-log
	touch /etc/ld.so.conf
	make install
	cp -v ../glibc-2.16.0/sunrpc/rpc/*.h /usr/include/rpc
	cp -v ../glibc-2.16.0/sunrpc/rpcsvc/*.h /usr/include/rpcsvc
	cp -v ../glibc-2.16.0/nis/rpcsvc/*.h /usr/include/rpcsvc
	mkdir -pv /usr/lib/locale
	localedef -i cs_CZ -f UTF-8 cs_CZ.UTF-8
	localedef -i de_DE -f ISO-8859-1 de_DE
	localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro
	localedef -i de_DE -f UTF-8 de_DE.UTF-8
	localedef -i en_GB -f UTF-8 en_GB.UTF-8
	localedef -i en_HK -f ISO-8859-1 en_HK
	localedef -i en_PH -f ISO-8859-1 en_PH
	localedef -i en_US -f ISO-8859-1 en_US
	localedef -i en_US -f UTF-8 en_US.UTF-8
	localedef -i es_MX -f ISO-8859-1 es_MX
	localedef -i fa_IR -f UTF-8 fa_IR
	localedef -i fr_FR -f ISO-8859-1 fr_FR
	localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro
	localedef -i fr_FR -f UTF-8 fr_FR.UTF-8
	localedef -i it_IT -f ISO-8859-1 it_IT
	localedef -i it_IT -f UTF-8 it_IT.UTF-8
	localedef -i ja_JP -f EUC-JP ja_JP
	localedef -i ru_RU -f KOI8-R ru_RU.KOI8-R
	localedef -i ru_RU -f UTF-8 ru_RU.UTF-8
	localedef -i tr_TR -f UTF-8 tr_TR.UTF-8
	localedef -i zh_CN -f GB18030 zh_CN.GB18030
	cat > /etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

	tar -xf ../tzdata2012f.tar.gz

	ZONEINFO=/usr/share/zoneinfo
	mkdir -pv $ZONEINFO/{posix,right} 

	for tz in etcetera southamerica northamerica europe africa antarctica  \
			  asia australasia backward pacificnew solar87 solar88 solar89 \
			  systemv; do
		zic -L /dev/null   -d $ZONEINFO       -y "sh yearistype.sh" ${tz} 
		zic -L /dev/null   -d $ZONEINFO/posix -y "sh yearistype.sh" ${tz} 
		zic -L leapseconds -d $ZONEINFO/right -y "sh yearistype.sh" ${tz}
	done

	cp -v zone.tab iso3166.tab $ZONEINFO
	zic -d $ZONEINFO -p America/New_York
	unset ZONEINFO
	cp -v --remove-destination /usr/share/zoneinfo/Europe/Bucharest \
		/etc/localtime
	
	cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib

EOF
	cat >> /etc/ld.so.conf << "EOF"
# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
	mkdir /etc/ld.so.conf.d
	
rDir

echo	"
Now that the final C libraries have been installed, 
it is time to adjust the toolchain so that it will 
link any newly compiled program against these 
new libraries.
First, backup the /tools linker, and replace it 
with the adjusted linker we made in chapter 5. 
We'll also create a link to its counterpart in 
/tools/$(gcc -dumpmachine)/bin"
mv -v /tools/bin/{ld,ld-old}
mv -v /tools/$(gcc -dumpmachine)/bin/{ld,ld-old}
mv -v /tools/bin/{ld-new,ld}
ln -sv /tools/bin/ld /tools/$(gcc -dumpmachine)/bin/ld

echo	"
Next, amend the GCC specs file so that it
points to the new dynamic linker. Simply 
deleting all instances of “/tools” should
leave us with the correct path to the dynamic
linker. Also adjust the specs file so that
GCC knows where to find the correct headers
and Glibc start files."
gcc -dumpspecs | sed -e 's@/tools@@g' \
    -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' > \
    `dirname $(gcc --print-libgcc-file-name)`/specs




# ===============================================================
# CHECKPOINT

echo	"
It is imperative at this point to ensure that the basic
functions (compiling and linking) of the adjusted 
toolchain are working as expected. "
cd /sources
echo 'main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

echo	"
If everything is working correctly, 
there should be no errors, and
the output of the last command will 
be (allowing for platform-specific 
differences in dynamic linker name):
	[Requesting program interpreter: /lib/ld-linux.so.2]
Note that /lib is now the prefix of our dynamic linker. "

echo	"
Now make sure that we're setup to use the correct startfiles:"

grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

echo	"
If everything is working correctly, there
should be no errors, and the output of
the last command will be:
	/usr/lib/crt1.o succeeded
	/usr/lib/crti.o succeeded
	/usr/lib/crtn.o succeeded"

echo	"
Verify that the compiler is searching for 
the correct header files:"

grep -B1 '^ /usr/include' dummy.log

echo	"
This command should return successfully
with the following output:
	#include <...> search starts here:
	/usr/include"


echo	"
Next, verify that the new linker is
being used with the correct search paths:"

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'

echo	"
If everything is working correctly, there
should be no errors, and the output of the
last command (allowing for platform-specific
target triplets) will be:
	SEARCH_DIR(/tools/i686-pc-linux-gnu/lib)
	SEARCH_DIR(/usr/lib)
	SEARCH_DIR(/lib);"

echo	"
Next make sure that we're using the correct libc:"

grep "/lib.*/libc.so.6 " dummy.log

echo	"
If everything is working correctly, there should
be no errors, and the output of the last
command (allowing for a lib64 directory
on 64-bit hosts) will be:
	attempt to open /lib/libc.so.6 succeeded"

echo	"
Lastly, make sure GCC is using the correct dynamic linker:"

grep found dummy.log

echo	"
If everything is working correctly, there
should be no errors, and the output of the
last command will be (allowing for platform-specific
differences in dynamic linker name and a
lib64 directory on 64-bit hosts):
	found ld-linux.so.2 at /lib/ld-linux.so.2"

rm -v dummy.c a.out dummy.log


echo	"
If the output does not appear as shown above or 
is not received at all, then something is seriously 
wrong. Investigate and retrace the steps to find 
out where the problem is and correct it. The 
most likely reason is that something went wrong 
with the specs file adjustment. Any issues will 
need to be resolved before continuing on 
with the process. 

Press CTRL+C to stop.
Press RETURN to continue.
"

read dummy

# CHECKPOINT
# ===============================================================




doItAll "6.11" "zlib-1.2.7" "bz2" 
mv -v /usr/lib/libz.so.* /lib
ln -sfv ../../lib/libz.so.1.2.7 /usr/lib/libz.so

doItAll "6.12" "file-5.11" "gz"

exp=$(expect -c "spawn ls")
if [ "$exp" != "spawn ls" ]; then
	rm -fv etc/standards.info
	sed -i.bak '/^INFO/s/standards.info //' etc/Makefile.in
	patch -Np1 -i ../binutils-2.22-build_fix-1.patch
	mkdir -v ../binutils-build
	cd ../binutils-build
	../binutils-2.22/configure --prefix=/usr --enable-shared
	make tooldir=/usr
	make -k check
	make tooldir=/usr install
	cp -v ../binutils-2.22/include/libiberty.h /usr/include
fi

initDir "6.14." "gmp-5.0.5" "xz"
	ABI=32 ./configure --prefix=/usr --enable-cxx --enable-mpbsd
	make  >> $CRTLOG
	make check 2>&1 | tee gmp-check-log
	awk '/tests passed/{total+=$2} ; END{print total}' gmp-check-log
	make install >> $CRTLOG
	if $MAKE_DOC; then
		mkdir -v /usr/share/doc/gmp-5.0.5
		cp    -v doc/{isa_abi_headache,configuration} doc/*.html \
			 /usr/share/doc/gmp-5.0.5
	fi
rDir


initDir "6.15." "mpfr-3.1.1" "xz"
	./configure  --prefix=/usr        \
             --enable-thread-safe \
             --docdir=/usr/share/doc/mpfr-3.1.1
finishDir
if $MAKE_DOC; then
	make html
	make install-html
fi

doItAll "6.16" "mpc-1.0.1" "gz"

initDir "5.9." "gcc-4.7.2" "bz2" "gcc-build"
	pushd $SRC_DIR > /dev/null
	sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
	case `uname -m` in
	  i?86) sed -i 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in ;;
	esac
	sed -i -e /autogen/d -e /check.sh/d fixincludes/Makefile.in
	popd
	/sources/$CRTPKG/configure --prefix=/usr            \
                       --libexecdir=/usr/lib    \
                       --enable-shared          \
                       --enable-threads=posix   \
                       --enable-__cxa_atexit    \
                       --enable-clocale=gnu     \
                       --enable-languages=c,c++ \
                       --disable-multilib       \
                       --disable-bootstrap      \
                       --with-system-zlib >> $CRTLOG
	make >> $CRTLOG
	ulimit -s 32768
	make -k check >> $CRTLOG
	/sources/$CRTPKG/contrib/test_summary
	make install >> $CRTLOG
	ln -sv ../usr/bin/cpp /lib
	ln -sv gcc /usr/bin/cc
	
rDir




# ===============================================================
# CHECKPOINT

echo	"
It is imperative at this point to ensure that the basic
functions (compiling and linking) of the adjusted 
toolchain are working as expected. "
cd /sources
echo 'main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose &> dummy.log
readelf -l a.out | grep ': /lib'

echo	"
If everything is working correctly, 
there should be no errors, and
the output of the last command will 
be (allowing for platform-specific 
differences in dynamic linker name):
	[Requesting program interpreter: /lib/ld-linux.so.2]
Note that /lib is now the prefix of our dynamic linker. "

echo	"
Now make sure that we're setup to use the correct startfiles:"

grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log

echo	"
If everything is working correctly, there
should be no errors, and the output of
the last command will be:
	/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../crt1.o succeeded
	/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../crti.o succeeded
	/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/../../../crtn.o succeeded
"

echo	"
Verify that the compiler is searching for 
the correct header files:"

grep -B4 '^ /usr/include' dummy.log

echo	"
This command should return successfully
with the following output:
	#include <...> search starts here:
	/usr/local/include
	/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/include
	/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/include-fixed
	/usr/include"


echo	"
Next, verify that the new linker is
being used with the correct search paths:"

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'

echo	"
If everything is working correctly, there
should be no errors, and the output of the
last command (allowing for platform-specific
target triplets) will be:
	SEARCH_DIR(/tools/i686-pc-linux-gnu/lib)
	SEARCH_DIR(/usr/local/lib)
	SEARCH_DIR(/lib)
	SEARCH_DIR(/usr/lib)"

echo	"
Next make sure that we're using the correct libc:"

grep "/lib.*/libc.so.6 " dummy.log

echo	"
If everything is working correctly, there should
be no errors, and the output of the last
command (allowing for a lib64 directory
on 64-bit hosts) will be:
	attempt to open /lib/libc.so.6 succeeded"

echo	"
Lastly, make sure GCC is using the correct dynamic linker:"

grep found dummy.log

echo	"
If everything is working correctly, there
should be no errors, and the output of the
last command will be (allowing for platform-specific
differences in dynamic linker name and a
lib64 directory on 64-bit hosts):
	found ld-linux.so.2 at /lib/ld-linux.so.2"

rm -v dummy.c a.out dummy.log


echo	"
If the output does not appear as shown above or 
is not received at all, then something is seriously 
wrong. Investigate and retrace the steps to find 
out where the problem is and correct it. The 
most likely reason is that something went wrong 
with the specs file adjustment. Any issues will 
need to be resolved before continuing on 
with the process. 

Press CTRL+C to stop.
Press RETURN to continue.
"

read dummy

# CHECKPOINT
# ===============================================================

# move a misplaced file
mkdir -pv /usr/share/gdb/auto-load/usr/lib
mv -v /usr/lib/*gdb.py /usr/share/gdb/auto-load/usr/lib



initDir "6.18." "sed-4.2.1" "bz2"
	patch -Np1 -i ../sed-4.2.1-testsuite_fixes-1.patch >> $CRTLOG
	./configure --prefix=/usr --bindir=/bin \
	   --htmldir=/usr/share/doc/sed-4.2.1 >> $CRTLOG
	make >> $CRTLOG
	if $MAKE_DOC; then
		make html >> $CRTLOG
	fi
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		make -C doc install-html >> $CRTLOG
	fi
rDir

initDir "6.19." "sed-4.2.1" "bz2"
	patch -Np1 -i ../bzip2-1.0.6-install_docs-1.patch >> $CRTLOG
	sed -i 's@\(ln -s -f \)$(PREFIX)/bin/@\1@' Makefile >> $CRTLOG
	sed -i "s@(PREFIX)/man@(PREFIX)/share/man@g" Makefile >> $CRTLOG
	make -f Makefile-libbz2_so >> $CRTLOG
	make clean >> $CRTLOG
	make >> $CRTLOG
	make PREFIX=/usr install >> $CRTLOG
	cp -v bzip2-shared /bin/bzip2 >> $CRTLOG
	cp -av libbz2.so* /lib       >> $CRTLOG
	ln -sv ../../lib/libbz2.so.1.0 /usr/lib/libbz2.so >> $CRTLOG
	rm -v /usr/bin/{bunzip2,bzcat,bzip2} >> $CRTLOG
	ln -sv bzip2 /bin/bunzip2 >> $CRTLOG
	ln -sv bzip2 /bin/bzcat >> $CRTLOG
rDir

initDir "6.20." "pkg-config-0.27.1" "gz"
	./configure --prefix=/usr         \
            --with-internal-glib  \
            --docdir=/usr/share/doc/pkg-config-0.27.1 >> $CRTLOG
finishDir

initDir "6.21." "ncurses-5.9" "gz"
	./configure --prefix=/usr           \
            --mandir=/usr/share/man \
            --with-shared           \
            --without-debug         \
            --enable-widec >> $CRTLOG
    make >> $CRTLOG
    make install >> $CRTLOG
	mv -v /usr/lib/libncursesw.so.5* /lib
	ln -sfv ../../lib/libncursesw.so.5 /usr/lib/libncursesw.so
	for lib in ncurses form panel menu ; do 
		rm -vf /usr/lib/lib${lib}.so 
		echo "INPUT(-l${lib}w)" >/usr/lib/lib${lib}.so
		ln -sfv lib${lib}w.a /usr/lib/lib${lib}.a
	done

	ln -sfv libncurses++w.a /usr/lib/libncurses++.a
	rm -vf                     /usr/lib/libcursesw.so
	echo "INPUT(-lncursesw)" > /usr/lib/libcursesw.so
	ln -sfv libncurses.so      /usr/lib/libcurses.so
	ln -sfv libncursesw.a      /usr/lib/libcursesw.a
	ln -sfv libncurses.a       /usr/lib/libcurses.a
	
	if $MAKE_DOC; then
		mkdir -v       /usr/share/doc/ncurses-5.9
		cp -v -R doc/* /usr/share/doc/ncurses-5.9
	fi
rDir


initDir "6.22." "util-linux-2.22.1" "xz"
	sed -i -e 's@etc/adjtime@var/lib/hwclock/adjtime@g' \
		 $(grep -rl '/etc/adjtime' .)
	mkdir -pv /var/lib/hwclock
	./configure --disable-su --disable-sulogin --disable-login >> $CRTLOG
	make  >> $CRTLOG
	make install >> $CRTLOG
rDir


initDir "6.23." "psmisc-22.20" "gz"
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	mv -v /usr/bin/fuser   /bin
	mv -v /usr/bin/killall /bin
rDir

initDir "6.24." "e2fsprogs-1.42.5" "gz"
	mkdir -v build
	cd build
	../configure --prefix=/usr         \
             --with-root-prefix="" \
             --enable-elf-shlibs   \
             --disable-libblkid    \
             --disable-libuuid     \
             --disable-uuidd       \
             --disable-fsck >> $CRTLOG
	make >> $CRTLOG
	make -k check >> $CRTLOG
	make install >> $CRTLOG
	make install-libs >> $CRTLOG
	chmod -v u+w /usr/lib/{libcom_err,libe2p,libext2fs,libss}.a >> $CRTLOG
	gunzip -v /usr/share/info/libext2fs.info.gz >> $CRTLOG
	install-info --dir-file=/usr/share/info/dir /usr/share/info/libext2fs.info >> $CRTLOG
	if $MAKE_DOC; then
		makeinfo -o      doc/com_err.info ../lib/et/com_err.texinfo >> $CRTLOG
		install -v -m644 doc/com_err.info /usr/share/info >> $CRTLOG
		install-info --dir-file=/usr/share/info/dir /usr/share/info/com_err.info >> $CRTLOG
	fi
rDir
initDir "6.25." "shadow-4.1.5.1" "bz2"
	sed -i 's/groups$(EXEEXT) //' src/Makefile.in
	find man -name Makefile.in -exec sed -i 's/groups\.1 / /' {} \;
	sed -i -e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA512@' \
       -e 's@/var/spool/mail@/var/mail@' etc/login.defs
    ./configure --sysconfdir=/etc >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
	mv -v /usr/bin/passwd /bin
	
	# TODO : should we enable shadow?
	# pwconv
	# grpconv
	passwd root
	
rDir
initDir "6.26." "coreutils-8.19" "xz"
	sed -i -e 's/! isatty/isatty/' \
       -e '45i\              || errno == ENOENT' gnulib-tests/test-getlogin.c
    patch -Np1 -i ../coreutils-8.19-i18n-1.patch
    FORCE_UNSAFE_CONFIGURE=1 ./configure \
            --prefix=/usr         \
            --libexecdir=/usr/lib \
            --enable-no-install-program=kill,uptime >> $CRTLOG
    
	make >> $CRTLOG
	make NON_ROOT_USERNAME=nobody check-root >> $CRTLOG
	echo "dummy:x:1000:nobody" >> /etc/group
	chown -Rv nobody . 
	su nobody -s /bin/bash \
          -c "PATH=$PATH make RUN_EXPENSIVE_TESTS=yes -k check || true"
    sed -i '/dummy/d' /etc/group
	make install >> $CRTLOG
	mv -v /usr/bin/{cat,chgrp,chmod,chown,cp,date,dd,df,echo} /bin
	mv -v /usr/bin/{false,ln,ls,mkdir,mknod,mv,pwd,rm} /bin
	mv -v /usr/bin/{rmdir,stty,sync,true,uname} /bin
	mv -v /usr/bin/chroot /usr/sbin
	mv -v /usr/share/man/man1/chroot.1 /usr/share/man/man8/chroot.8
	sed -i s/\"1\"/\"8\"/1 /usr/share/man/man8/chroot.8
	mv -v /usr/bin/{head,sleep,nice} /bin
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.27." "iana-etc-2.30" "bz2"
	make >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.28." "m4-1.4.16" "bz2"
	sed -i -e '/gets is a/d' lib/stdio.in.h
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	sed -i -e '41s/ENOENT/& || errno == EINVAL/' tests/test-readlink.h
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.29." "bison-2.6.2" "xz"
	./configure --prefix=/usr >> $CRTLOG
	echo '#define YYENABLE_NLS 1' >> lib/config.h
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.30." "procps-3.2.8" "gz"
	patch -Np1 -i ../procps-3.2.8-fix_HZ_errors-1.patch
	patch -Np1 -i ../procps-3.2.8-watch_unicode-1.patch
	sed -i -e 's@\*/module.mk@proc/module.mk ps/module.mk@' \
		   -e 's@^#SKIP@SKIP@' Makefile
	make >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.31." "grep-2.14" "xz"
	./configure --prefix=/usr --bindir=/bin >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.32." "readline-6.2" "gz"
	sed -i '/MV.*old/d' Makefile.in
	sed -i '/{OLDSUFF}/c:' support/shlib-install
	patch -Np1 -i ../readline-6.2-fixes-1.patch
	./configure --prefix=/usr --libdir=/lib >> $CRTLOG
	make SHLIB_LIBS=-lncurses >> $CRTLOG
	make install >> $CRTLOG
	mv -v /lib/lib{readline,history}.a /usr/lib
	rm -v /lib/lib{readline,history}.so
	ln -sfv ../../lib/libreadline.so.6 /usr/lib/libreadline.so
	ln -sfv ../../lib/libhistory.so.6 /usr/lib/libhistory.so
	if $MAKE_DOC; then
		mkdir   -v       /usr/share/doc/readline-6.2
		install -v -m644 doc/*.{ps,pdf,html,dvi} \
						 /usr/share/doc/readline-6.2
	fi
rDir
initDir "6.33." "bash-4.2" "gz"
	patch -Np1 -i ../bash-4.2-fixes-9.patch
	./configure --prefix=/usr                     \
				--bindir=/bin                     \
				--htmldir=/usr/share/doc/bash-4.2 \
				--without-bash-malloc             \
				--with-installed-readline >> $CRTLOG
	make >> $CRTLOG
	chown -Rv nobody .
	su nobody -s /bin/bash -c "PATH=$PATH make tests"
	make install >> $CRTLOG
	exec /bin/bash --login +h
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.34." "libtool-2.4.2" "gz"
	./configure --prefix=/usr
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.35." "gdbm-1.10" "gz"
	./configure --prefix=/usr --enable-libgdbm-compat >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.36." "inetutils-1.9.1" "gz"
	sed -i -e '/gets is a/d' lib/stdio.in.h
	./configure --prefix=/usr  \
		--libexecdir=/usr/sbin \
		--localstatedir=/var   \
		--disable-ifconfig     \
		--disable-logger       \
		--disable-syslogd      \
		--disable-whois        \
		--disable-servers >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		make -C doc html
		make -C doc install-html docdir=/usr/share/doc/inetutils-1.9.1
	fi
	mv -v /usr/bin/{hostname,ping,ping6} /bin
	mv -v /usr/bin/traceroute /sbin
rDir
initDir "6.37." "perl-5.16.1" "bz2"
	echo "127.0.0.1 localhost $(hostname)" > /etc/hosts
	sed -i -e "s|BUILD_ZLIB\s*= True|BUILD_ZLIB = False|"           \
       -e "s|INCLUDE\s*= ./zlib-src|INCLUDE    = /usr/include|" \
       -e "s|LIB\s*= ./zlib-src|LIB        = /usr/lib|"         \
    cpan/Compress-Raw-Zlib/config.in
    sh Configure -des -Dprefix=/usr                 \
                  -Dvendorprefix=/usr           \
                  -Dman1dir=/usr/share/man/man1 \
                  -Dman3dir=/usr/share/man/man3 \
                  -Dpager="/usr/bin/less -isR"  \
                  -Duseshrplib >> $CRTLOG
	make >> $CRTLOG
	make -k test >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.38." "autoconf-2.69" "xz"
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.39." "automake-1.12.4" "xz"
	./configure --prefix=/usr --docdir=/usr/share/doc/automake-1.12.4 >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.40." "diffutils-3.2" "gz"
	sed -i -e '/gets is a/d' lib/stdio.in.h
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.41." "gawk-4.0.1" "xz"
	./configure --prefix=/usr --libexecdir=/usr/lib >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		mkdir -v /usr/share/doc/gawk-4.0.1
		cp    -v doc/{awkforai.txt,*.{eps,pdf,jpg}} /usr/share/doc/gawk-4.0.1
	fi
rDir
initDir "6.42." "findutils-4.4.2" "gz"
	./configure --prefix=/usr                   \
				--libexecdir=/usr/lib/findutils \
				--localstatedir=/var/lib/locate >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
	mv -v /usr/bin/find /bin
	sed -i 's/find:=${BINDIR}/find:=\/bin/' /usr/bin/updatedb
rDir
initDir "6.43." "flex-2.5.37" "bz2"
	patch -Np1 -i ../flex-2.5.37-bison-2.6.1-1.patch
	./configure --prefix=/usr             \
            --mandir=/usr/share/man   \
            --infodir=/usr/share/info \
            --docdir=/usr/share/doc/flex-2.5.37 >> $CRTLOG
	make >> $CRTLOG
	make -k check >> $CRTLOG
	make install >> $CRTLOG
	ln -sv libfl.a /usr/lib/libl.a
	cat > /usr/bin/lex << "EOF"
#!/bin/sh
# Begin /usr/bin/lex

exec /usr/bin/flex -l "$@"

# End /usr/bin/lex
EOF
	chmod -v 755 /usr/bin/lex
	if $MAKE_DOC; then
		cp -v doc/flex.pdf /usr/share/doc/flex-2.5.37
	fi
rDir
initDir "6.44." "gettext-0.18.1.1" "gz"
	sed -i -e '/gets is a/d' gettext-*/*/stdio.in.h
	./configure --prefix=/usr \
            --docdir=/usr/share/doc/gettext-0.18.1.1 >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.45." "groff-1.21" "gz"
	PAGE=<paper_size> ./configure --prefix=/usr
	make >> $CRTLOG
	make install >> $CRTLOG
	ln -sv eqn /usr/bin/geqn
	ln -sv tbl /usr/bin/gtbl
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.46." "xz-5.0.4" "xz"
	./configure --prefix=/usr --libdir=/lib \
		--docdir=/usr/share/doc/xz-5.0.4  >> $CRTLOG
	make >> $CRTLOG
	make pkgconfigdir=/usr/lib/pkgconfig install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.47." "grub-2.00" "xz"
	sed -i -e '/gets is a/d' grub-core/gnulib/stdio.in.h
	./configure --prefix=/usr          \
            --sysconfdir=/etc      \
            --disable-grub-emu-usb \
            --disable-efiemu       \
            --disable-werror >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.48." "less-451" "gz"
	./configure --prefix=/usr --sysconfdir=/etc >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.49." "gzip-1.5" "xz"
	./configure --prefix=/usr --bindir=/bin >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	mv -v /bin/{gzexe,uncompress,zcmp,zdiff,zegrep} /usr/bin
	mv -v /bin/{zfgrep,zforce,zgrep,zless,zmore,znew} /usr/bin
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.50." "iproute2-3.6.0" "xz"
	patch -Np1 -i ../iproute2-3.6.0-ipset-1.patch
	sed -i '/^TARGETS/s@arpd@@g' misc/Makefile
	sed -i /ARPD/d Makefile
	sed -i 's/arpd.8//' man/man8/Makefile
	make DESTDIR= >> $CRTLOG
	make DESTDIR=              \
     MANDIR=/usr/share/man \
     DOCDIR=/usr/share/doc/iproute2-3.6.0 install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.51." "kbd-1.15.3" "gz"
	patch -Np1 -i ../kbd-1.15.3-upstream_fixes-1.patch
	patch -Np1 -i ../kbd-1.15.3-backspace-1.patch
	sed -i '/guardado\ el/s/\(^.*en\ %\)\(.*\)/\14\$\2/' po/es.po
	sed -i 's/\(RESIZECONS_PROGS=\)yes/\1no/' configure &&
	sed -i 's/resizecons.8 //' man/man8/Makefile.in &&
	touch -d '2011-05-07 08:30' configure.ac
	./configure --prefix=/usr --datadir=/lib/kbd >> $CRTLOG
	make >> $CRTLOG
	make install >> $CRTLOG
	mv -v /usr/bin/{kbd_mode,loadkeys,openvt,setfont} /bin
	if $MAKE_DOC; then
		mkdir -v /usr/share/doc/kbd-1.15.3
		cp -R -v doc/* \
				 /usr/share/doc/kbd-1.15.3
	fi
rDir
initDir "6.52." "kmod-10" "xz"
	patch -Np1 -i ../kmod-10-testsuite-1.patch
	./configure --prefix=/usr       \
            --bindir=/bin       \
            --libdir=/lib       \
            --sysconfdir=/etc   \
            --with-xz           \
            --with-zlib >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make pkgconfigdir=/usr/lib/pkgconfig install >> $CRTLOG
	for target in depmod insmod modinfo modprobe rmmod; do
	  ln -sv ../bin/kmod /sbin/$target
	done
	ln -sv kmod /bin/lsmod
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.53." "libpipeline-1.2.2" "gz"
	PKG_CONFIG_PATH=/tools/lib/pkgconfig \
		./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.54." "make-3.82" "bz2"
	patch -Np1 -i ../make-3.82-upstream_fixes-3.patch
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.55." "man-db-2.6.3" "xz"
	./configure --prefix=/usr                        \
            --libexecdir=/usr/lib                \
            --docdir=/usr/share/doc/man-db-2.6.3 \
            --sysconfdir=/etc                    \
            --disable-setuid                     \
            --with-browser=/usr/bin/lynx         \
            --with-vgrind=/usr/bin/vgrind        \
            --with-grap=/usr/bin/grap >> $CRTLOG 
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.56." "patch-2.7.1" "xz"
	./configure --prefix=/usr >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.57." "sysklogd-1.5" "gz"
	
	make >> $CRTLOG
	make BINDIR=/sbin install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
	cat > /etc/syslog.conf << "EOF"
# Begin /etc/syslog.conf

auth,authpriv.* -/var/log/auth.log
*.*;auth,authpriv.none -/var/log/sys.log
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
*.emerg *

# End /etc/syslog.conf
EOF
	
rDir
initDir "6.58." "sysvinit-2.88dsf" "bz2"
	sed -i 's@Sending processes@& configured via /etc/inittab@g' src/init.c
	sed -i -e '/utmpdump/d' \
       -e '/mountpoint/d' src/Makefile
	make -C src >> $CRTLOG
	make -C src install >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.59." "tar-1.26" "bz2"
	sed -i -e '/gets is a/d' gnu/stdio.in.h
	FORCE_UNSAFE_CONFIGURE=1  \
	./configure --prefix=/usr \
            --bindir=/bin \
            --libexecdir=/usr/sbin >> $CRTLOG
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	make -C doc install-html docdir=/usr/share/doc/tar-1.26 >> $CRTLOG
	if $MAKE_DOC; then
		
	fi
rDir
initDirEx "6.60." "texinfo-4.13a" "gz" "texinfo-4.13"
	./configure --prefix=/usr
	make >> $CRTLOG
	make check >> $CRTLOG
	make install >> $CRTLOG
	make TEXMF=/usr/share/texmf install-tex
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.61." "systemd-193" "xz"
	tar -xvf ../udev-lfs-193.tar.bz2 >> $CRTLOG
	make -f udev-lfs-193/Makefile.lfs >> $CRTLOG
	make -f udev-lfs-193/Makefile.lfs install >> $CRTLOG
	bash udev-lfs-193/init-net-rules.sh
	if $MAKE_DOC; then
		
	fi
rDir
initDir "6.61." "systemd-193" "xz"
	echo '#define SYS_VIMRC_FILE "/etc/vimrc"' >> src/feature.h
	./configure --prefix=/usr --enable-multibyte >> $CRTLOG
	make >> $CRTLOG
	make test >> $CRTLOG
	make install >> $CRTLOG
	ln -sv vim /usr/bin/vi
	for L in  /usr/share/man/{,*/}man1/vim.1; do
		ln -sv vim.1 $(dirname $L)/vi.1
	done
	ln -sv ../vim/vim73/doc /usr/share/doc/vim-7.3
	cat > /etc/vimrc << "EOF"
" Begin /etc/vimrc

set nocompatible
set backspace=2
syntax on
if (&term == "iterm") || (&term == "putty")
  set background=dark
endif

" End /etc/vimrc
EOF
rDir


echo	""
echo	""
echo	"---------------------------------------------"
echo	"  Stripping Again"
echo	"---------------------------------------------"
echo	""
echo	""

/tools/bin/find /{,usr/}{bin,lib,sbin} -type f \
  -exec /tools/bin/strip --strip-debug '{}' ';' >> /dev/null




logout




